{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "269621cf-4bc0-4ff2-b4b0-b5462fc12a79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\"\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "15a54075-115c-4b5f-9767-4953db5fcb65",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_hub.tools.notion.base import NotionToolSpec\n",
    "\n",
    "notion_token = \"secret_your-key\"\n",
    "tool_spec = NotionToolSpec(integration_token=notion_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5035ed20-6d50-4d01-be4d-72139b401f75",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the Agent with our tools\n",
    "agent = OpenAIAgent.from_tools(tool_spec.to_tool_list(), verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2ef1b0bd-2b5f-4185-b98d-68680145182d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: append_data with args: {\n",
      "  \"block_id\": \"movies-page-id\",\n",
      "  \"content\": \"I Am Legend\",\n",
      "  \"text_type\": \"heading_1\"\n",
      "}\n",
      "Got output: This endpoint only supports block ids in the form of a uuid as input. Please use the search_data endpoint to find the id of the page you are looking for and then call this endpoint again\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: search_data with args: {\n",
      "  \"query\": \"movies\"\n",
      "}\n",
      "Got output: ['7e034e22-4511-487e-a7a2-13b1a2ad506e']\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: append_data with args: {\n",
      "  \"block_id\": \"7e034e22-4511-487e-a7a2-13b1a2ad506e\",\n",
      "  \"content\": \"I Am Legend\",\n",
      "  \"text_type\": \"heading_1\"\n",
      "}\n",
      "Got output: success\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='The heading \"I Am Legend\" has been appended to the movies page.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat('append the heading \"I Am Legend\" to the movies page')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c50d26ef-862c-4535-b780-0dcc6666b36a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: append_data with args: {\n",
      "  \"block_id\": \"7e034e22-4511-487e-a7a2-13b1a2ad506e\",\n",
      "  \"content\": \"Summary: I Am Legend is a 2007 American post-apocalyptic action thriller film based on the 1954 novel of the same name by Richard Matheson. The film follows a scientist in New York City who is the last human survivor of a plague that turns people into aggressive, vampire-like creatures. He spends his days trying to find a cure for the virus and his nights defending himself against the creatures. The film stars Will Smith as the main character, Robert Neville.\",\n",
      "  \"text_type\": \"paragraph\"\n",
      "}\n",
      "Got output: success\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='A summary of \"I Am Legend\" has been appended to the movies page.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"Append a summary of I Am Legend to the page\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "661a14da-022e-4365-ba96-25c9f105c2e2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
